package com.jun.flower.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jun.flower.bean.PageResult;
import com.jun.flower.bean.TbFlowers;
import com.jun.flower.bean.TbFlowersExample;
import com.jun.flower.bean.TbFlowersExample.Criteria;
import com.jun.flower.dao.TbFlowersMapper;
import com.jun.flower.service.FlowerService;

@Service
public class FlowerServiceImpl implements FlowerService {
	@Autowired
	private TbFlowersMapper flowersMapper;
	
	public TbFlowers getFlowerById(Integer id) {
		TbFlowers flower=flowersMapper.selectByPrimaryKey(id);
		return flower;
	}

	@Override
	public PageResult<TbFlowers> getFlowerList(Integer pageNum, Integer pageSize) {
		//分页处理
		PageHelper.startPage(pageNum,pageSize);
		//执行查询
		List<TbFlowers> list=flowersMapper.selectByExample(null);
		//取分页结果
		PageInfo<TbFlowers> pageInfo = new PageInfo<>(list);
		//取总记录数
		long total = pageInfo.getTotal();
		//取总页数
		int pages = pageInfo.getPages();
		//创建一个PageResult对象
		PageResult<TbFlowers> pageResult = new PageResult<>();
		pageResult.setList(list);
		pageResult.setPageNum(pageNum);
		pageResult.setPages(pages);
		pageResult.setPageSize(pageSize);
		pageResult.setTotal(total);
		return pageResult;
	}

	@Override
	public boolean checkFlowerNum(String flowerNum) {
		//根据鲜花编号查询鲜花数据
		TbFlowersExample example = new TbFlowersExample();
		Criteria criteria = example.createCriteria();
		criteria.andFlowernumEqualTo(flowerNum);
		//执行查询
		List<TbFlowers> list = flowersMapper.selectByExample(example);
		if(list == null || list.size()==0) {
			//如果没有查询到数据返回true
			return true;
		}
		//如果查询到数据返回false
		return false;
	}

	@Override
	public void addFlower(TbFlowers flower) {
		flowersMapper.insert(flower);
	}
	
	@Override
	public Boolean deleteFlower(String ids) {
		// 1、把字符串进行字符串拆分，可以得到一个字符串数组。
		String[] idsArr = ids.split(",");
		// 2、遍历数组把每个元素转换成Integer类型，并且放到一个list中
		List<Integer> idsList = new ArrayList<>();
		for (String string : idsArr) {
			Integer id = Integer.parseInt(string);
			idsList.add(id);
		}
		// 3、创建查询条件
		TbFlowersExample example = new TbFlowersExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdIn(idsList);
		// 4、执行删除操作
		flowersMapper.deleteByExample(example);
		// 5、返回结果
		return true;
	}

	@Override
	public List<TbFlowers> orderGetFlowerList() {
		List<TbFlowers> list = flowersMapper.selectByExample(null);
		return list;
	}

	@Override
	public void updateFlowerById(TbFlowers flower) {
		flowersMapper.updateByPrimaryKey(flower);
	}

	@Override
	public List<TbFlowers> getWebFlowerList() {
		List<TbFlowers> list = flowersMapper.selectByExample(null);
		return list;
	}

}
